import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:projectfluttershengyihuatong/service/jsontool.dart';
import 'package:projectfluttershengyihuatong/service/loginService/UserInfoModel.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../request_manager.dart';
import '../launchservice/launchservice.dart';
class LoginService extends GetxController{

  /// 单例对象
  static LoginService? _instance;

  /// 内部构造方法，可避免外部暴露构造函数，进行实例化
  LoginService._internal();

  /// 工厂构造方法，这里使用命名构造函数方式进行声明
  factory LoginService.getInstance() => _getInstance();

  /// 获取单例内部方法
  static _getInstance() {
    // 只能有一个实例
    _instance ??= LoginService._internal();
    return _instance;
  }
  UserInfoModel? userInfo;
  bool isShowLoginPage = false;
  static const LOGIN = "firstLogin";
  ///不是开机启动的时候登录
  static const LOGIN2 = "secondLogin";



  static Future<BaseModel<UserInfoModel>> login(String phone, String password) async{
    var api = LaunchService.getInstance().config?.api?.mobilePasswdLogin;
    if (api == null) {
      if (kDebugMode) {
        print("没有登录api");
      }
      return BaseModel(ret: 0, msg: "没有对应的api",data: null);
    }

    var deviceInfo = DeviceInfoPlugin();
    String systemVersion = "";
    String deviceModel = "";
    if (Platform.isIOS) {
      var device = await deviceInfo.iosInfo;
      deviceModel = device.model;
      systemVersion = device.systemVersion;
    }else if (Platform.isAndroid) {
      var device = await deviceInfo.androidInfo;
      deviceModel = device.model;
      systemVersion = device.device;

    }else  {
      deviceModel = "IOS";
      systemVersion = "13.0";
    }

    PackageInfo packageInfo = await PackageInfo.fromPlatform();

    String appName = packageInfo.appName;
    String packageName = packageInfo.packageName;
    String version = packageInfo.version;
    String buildNumber = packageInfo.buildNumber;

    var param = {"mobile": phone,
      "passwd": password,
      "clientType": 1,
      "deviceModel": deviceModel,
      "appVersion": version,
      "systemVersion": systemVersion};
    var response = await RequestManager.getInstance().dio.get(api ?? "", queryParameters: param);
    var data = response.toString();
    final result = jsonDecode(data);

    var saveManager = await SharedPreferences.getInstance();
    final model = BaseModel<UserInfoModel>.fromJson(result);
    LoginService.getInstance().userInfo = model.data;
    LoginService.getInstance().update();
    final bo = await saveManager.setString("userinfo", data);
    final bo2 = await saveManager.setString('phone', phone);
    return model;
  }
  getPhone() async {
    var saveManager = await SharedPreferences.getInstance();
    var phone = saveManager.getString("phone") ?? "";
    this.phone = phone;
    LoginService.getInstance().update([LoginService.savePhone]);
  }
  static String savePhone = "savePhone";
  var phone = "";










  Future<UserInfoModel?> getUserInfo() async {
    final manager = await SharedPreferences.getInstance();
    var value = manager.getString("userinfo");

    if (value != null) {
      var result = jsonDecode(value);
      final baseModel = BaseModel<UserInfoModel>.fromJson(result);
      LoginService.getInstance().userInfo = baseModel.data;
      LoginService.getInstance().update();

      return baseModel.data;
    }else {
      return null;
    }

  }
}